<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('证券账户信息') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.prevent.native>
      <el-form-item :label="$t('证券账号')" prop="securityAccount">
        <el-input
          v-model="queryParams.securityAccount"
          :placeholder="$t('证券账号')"
          clearable
          :onsubmit="false"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('证券公司名称')" prop="securityName">
        <el-input
          v-model="queryParams.securityName"
          :placeholder="$t('证券公司名称')"
          clearable
          :onsubmit="false"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('存管账户开户行')" prop="bankAccount">
        <el-input
          v-model="queryParams.bankAccount"
          :placeholder="$t('开户行')"
          clearable
          :onsubmit="false"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['listed:security:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="listedSecurityList" :summary-method="getSummaries" show-summary border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('证券公司名称')" align="center" prop="securityName" show-overflow-tooltip min-width="150" />
      <el-table-column :label="$t('证券账号')" align="center" prop="securityAccount" show-overflow-tooltip min-width="140" />
      <el-table-column :label="$t('第三方存管账户')" align="center" prop="thirdAccount" show-overflow-tooltip min-width="130" />
      <el-table-column :label="$t('持仓合计') + $t('金额单位')" align="center" prop="totalPosition" show-overflow-tooltip width="120">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.totalPosition, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('存管账户开户行')" align="center" prop="bankAccount" show-overflow-tooltip min-width="130" />
      <el-table-column :label="$t('对应投资主体')" align="center" prop="investorName" show-overflow-tooltip min-width="150" />
      <el-table-column :label="$t('是否是创投基金')" align="center" prop="ventureFund" show-overflow-tooltip :formatter="ventureFundFormat" min-width="120" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="140">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['listed:security:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['listed:security:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改上市项目-证券账户管理对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <listedSecurityForm ref="listedSecurityForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listListedSecurity, delListedSecurity, exportListedSecurity } from '@/api/deal/listed/security/listedSecurity'
import listedSecurityForm from './listedSecurityForm.vue'
import { moneyFormat } from '@/utils/ruoyi'
export default {
    components: {
        listedSecurityForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 上市项目-证券账户管理表格数据
          listedSecurityList: [],
          // 弹出层标题
          title: '证券账户',
          // 是否显示弹出层
          formOpen: false,
          // 是否是创投基金字典
          ventureFundOptions: [],
          // 查询参数
          queryParams: {
              listedId: undefined,
              securityName: undefined,
              securityAccount: undefined,
              bankAccount: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
      this.getDicts('sys_yes_no').then(response => {
          this.ventureFundOptions = response.data
      })
  },
  methods: {
    /** 查询上市项目-证券账户管理列表 */
    getList() {
        this.loading = true
        this.queryParams.dealType = (this.$route.query.dealType === undefined ? 'equity' : this.$route.query.dealType)
        listListedSecurity(this.queryParams).then(response => {
            this.listedSecurityList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 是否是创投基金字典翻译
    ventureFundFormat(row) {
      return this.selectDictLabel(this.ventureFundOptions, row.ventureFund)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            listedId: undefined,
            securityName: undefined,
            securityAccount: undefined,
            thirdAccount: undefined,
            bankAccount: undefined,
            investorId: undefined,
            totalPosition: undefined,
            ventureFund: '0',
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
            remark: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.listedSecurityForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.listedSecurityForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delListedSecurity(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportListedSecurity(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = this.$t('合计')
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          // 持仓合计
          if (column.property === 'totalPosition') {
            sums[index] = moneyFormat(sums[index], 'w')
          } else {
            sums[index] = ''
          }
        } else {
          sums[index] = ''
        }
      })
      return sums
    }
  }
}
</script>
